package org.simtube.index;

import java.util.Random;

import org.simtube.util.DocItem;
import org.simtube.util.DocVector;

public class CSRTreeIndex implements Index {
	
	private DocItem[] docMat;
	private CSRTree tree;
	
	public CSRTreeIndex(DocItem[] docMat) {
		this.docMat = docMat;
		tree = new CSRTree(4, 8);
	}

	@Override
	public void buildIndex() {
		for(int i = 0; i < docMat.length; i++) {
			System.out.println("Adding element " + (i + 1));
			tree.insert(new Region(docMat[i].vector, 0), i);
		}
	}

	@Override
	public void loadIndex(String indexFile) {
	}

	@Override
	public int[] kNNQuery(int k, DocVector query) {
		return tree.kNNSearch(new Region(query, 0), k);
	}

	@Override
	public void setDocMat(DocItem[] docMat) {
		this.docMat = docMat;
	}
	
	public static void main(String[] args) {
		int n = 10;
		int dim = 256;
		Random rand = new Random();
		DocItem[] docm = new DocItem[n];
		
		double[] v = null;
		for(int i = 0; i < n; i++) {
			if(v == null) {
				v = new double[dim];
				for(int j = 0; j < dim; j++)
					v[j] = rand.nextDouble();
			} else v = v.clone();
			DocVector dv = new DocVector(v).normalize();
			docm[i] = new DocItem(dv, i);
		}
		
		CSRTreeIndex index = new CSRTreeIndex(docm);
		index.buildIndex();
		System.out.println(index.kNNQuery(10, docm[0].vector));
	}

}
